package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.MailAdapter;
import com.sx.zongzhi.bean.BoxSearchBean;
import com.sx.zongzhi.bean.MailBean;
import com.sx.zongzhi.presenter.SendBoxPest;
import com.sx.zongzhi.utils.AppConstants;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.ISendBoxView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 发件箱
 */
public class SendBoxActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, ISendBoxView {

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.imvAdd)
    ImageView imvAdd;
    @BindView(R.id.tv_Total)
    TextView tvTotal;
    private MailAdapter adapter;
    private List<MailBean> mailBeanList = new ArrayList<>();

    private int page = 1, rows = 10;
    private String searchTitle = "", searchTimeStart = "", searchTimeEnd = "";
    private Disposable disposable;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_household);
        setTitleName(R.string.send_box);
        ButterKnife.bind(this);
        setRightImage(R.mipmap.icon_nav_search);
        initView();
    }

    private void initView() {
        imvAdd.setVisibility(View.VISIBLE);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new MailAdapter(this, R.layout.item_send_mail_list, mailBeanList);
        adapter.setEmptyView(View.inflate(this, R.layout.load_error_view, null));
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
            }
        }, recyclerView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(SendBoxActivity.this, SendBoxDetailActivity.class)
                        .putExtra("messageId", mailBeanList.get(position).id));
            }
        });
        initRx();
        onRefresh();
    }

    private void initRx() {
        disposable = MyRxBus.get().toObservable(BoxSearchBean.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BoxSearchBean>() {
                    @Override
                    public void accept(BoxSearchBean bean) throws Exception {
                        if (bean != null && bean.mailType == 1) {
                            searchTitle = bean.searchTitle;
                            searchTimeStart = bean.searchTimeStart;
                            searchTimeEnd = bean.searchTimeEnd;
                            onRefresh();
                        }
                    }
                });
    }

    @OnClick({R.id.imv_Right, R.id.imv_Right2, R.id.imvAdd})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.imv_Right://搜索
                startActivity(new Intent(this, InBoxSearchActivity.class).putExtra("MAIL_TYPE", 1));
                break;
            case R.id.imvAdd:
                startActivity(new Intent(this, AddMailActivity.class));
                break;
        }
    }

    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(false);
        page = 1;
        searchTitle = "";
        searchTimeStart = "";
        searchTimeEnd = "";
        adapter.setEnableLoadMore(true);
        getData();
    }

    private void getData() {
        new SendBoxPest(this).getSendBoxList(this, page, rows, searchTitle, searchTimeStart, searchTimeEnd);
    }

    @Override
    public void getSendBoxListSuccess(List<MailBean> mailBeanLists,int total) {
        if (page == 1) {
            mailBeanList.clear();
            adapter.setNewData(mailBeanList);
        }
        if (mailBeanLists != null && mailBeanLists.size() > 0) {
            mailBeanList.addAll(mailBeanLists);
        }
        adapter.notifyDataSetChanged();
        tvTotal.setText( "共"+total + "条数据");
        if (mailBeanList.size() == total) {
            adapter.loadMoreEnd();
        }else {
            adapter.loadMoreComplete();
        }
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(SendBoxActivity.this, message);
    }
    @Override
    protected void onDestroy() {
        if (disposable != null){
            disposable.dispose();
        }
        mailBeanList = null;
        super.onDestroy();
    }
}
